<template>
  <div class = "picture-upload">
    <a-upload
        list-type="picture-card"
        :show-upload-list="false"
        :before-upload="beforeUpload"
        :custom-request="handleUpload"
    >
      <img v-if="picture?.url" :src="picture?.url" alt="avatar" />
      <div v-else>
        <loading-outlined v-if="loading"></loading-outlined>
        <plus-outlined v-else></plus-outlined>
        <div class="ant-upload-text" >点击或拖拽上传图片</div>
      </div>
    </a-upload>
  </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import type { UploadProps } from 'ant-design-vue';
import {uploadPictureUsingPost} from "@/api/pictureController.ts";


interface Props {
  picture?: API.PictureVO
  spaceId?: number
  onSuccess?: (newPicture: API.PictureVO) => void
}

const props = defineProps<Props>();


const handleUpload = async ({file} : any) => {
    loading.value = true;
    try{
      const params:API.PictureUploadRequest = props.picture?{id: props.picture.id} : {}
      params.spaceId = props.spaceId
      const res = await uploadPictureUsingPost(params,{},file)
      if(res.data.code === 200 && res.data.data){
        message.success("图片上传成功")
        //将上传成功的图片信息传递给父组件
        props.onSuccess?.(res.data.data)
      }else{
        message.error("图片上传失败" + res.data.message)
      }
    }catch (error){
      message.error("图片上传失败," + error.message)
    }finally {
      loading.value = false;
    }
};

const loading = ref<boolean>(false)


/**
 * 上传前的校验
 * @param file
 */
const beforeUpload = (file: UploadProps['fileList'][number]) => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/webp';
  if (!isJpgOrPng) {
    message.error('不支持上传的格式，支持jpg、png、webp!');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('图片大小不能超过2M!');
  }
  return isJpgOrPng && isLt2M;
};


</script>
<style scoped>
.picture-upload :deep(.ant-upload) {
  width: 100% !important;
  height: 100% !important;
  min-width: 152px;
  min-height: 152px;
}

.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
